﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MS.TravelJourney.Final.ViewModels;
using Microsoft.Phone.Tasks;
using Windows.Phone.System.UserProfile;
using System.Diagnostics;
using MS.TravelJourney.Final.Core;
using Windows.Phone.Speech.Synthesis;
using Windows.Phone.Storage.SharedAccess;

namespace MS.TravelJourney.Final
{
    public partial class ItemDetail : PhoneApplicationPage
    {
        #region Tasks
        SmsComposeTask SmsTask = null;
        ItemViewModel item = null;
        SpeechSynthesizer synth;
        #endregion
        public ItemDetail()
        {
            InitializeComponent();
            SmsTask = new SmsComposeTask();
            synth = new SpeechSynthesizer();
        }
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
                await App.ViewModel.LoadData();
            try
            {
                string name = this.NavigationContext.QueryString["name"];
                var result = from a in App.ViewModel.Items
                                where a.Name == name
                                select a;
                item = result.First();
            }
            catch (Exception)
            {
                NavigationService.GoBack();
            }
            this.DataContext = item;
            base.OnNavigatedTo(e);
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MapLocation.xaml",UriKind.Relative));                    
        }

        private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
        {
            SmsTask.Body = item.Desc;
            SmsTask.Show();
        }

        private void ApplicationBarIconButton_Click_3(object sender, EventArgs e)
        {
            LockHelper(item.Photo, true);
            MessageBox.Show("Success!");
        }

        private void ApplicationBarIconButton_Click_4(object sender, EventArgs e)
        {
            var uri = "/Mainpage.xaml";
            if (TileCore.TileExists(uri))
            {
                TileCore.DeleteTile(uri);
            }
            TileCore.SetTile(item, uri);
        }
        /// <summary>
        /// 设置锁屏背景
        /// </summary>
        /// <param name="filePathOfTheImage">本地图片路径</param>
        /// <param name="isAppResource"></param>
        private async void LockHelper(string filePathOfTheImage, bool isAppResource)
        {
            try
            {
                bool isProvider = LockScreenManager.IsProvidedByCurrentApplication;
                if (!isProvider)
                {
                    //设置此应用为锁屏提供者
                    LockScreenRequestResult op = await LockScreenManager.RequestAccessAsync();
                    //返回结果为：Granted，则设置成功； Denied, 则设置失败
                    isProvider = op == LockScreenRequestResult.Granted;
                }
                if (isProvider)
                {
                    // ms-appx:/// ：代表应用安装文件夹下的根目录
                    // ms-appdata:///Local/ ：代表的是应用独立存储区下的根目录
                    // 两者区别在于，前者图片在应用上传时就确定，后者可先从网络获取，再保持到独立存储器来设置背景
                    string shema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
                    var uri = new Uri(shema + filePathOfTheImage);

                    //设置锁屏背景源
                    LockScreen.SetImageUri(uri);
                }
                else
                {
                    Debug.WriteLine("设置成功！");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }

        private async void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            await synth.SpeakTextAsync(item.Desc);
        }
    }
}